package com.qlk.odcinterface.rest;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.resteasy.annotations.Form;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.qlk.common.utils.serialize.JsonHelper;
import com.qlk.common.utils.wrap.WrapMapper;
import com.qlk.common.utils.wrap.Wrapper;
import com.qlk.odcinterface.domain.PmsCoupon;
import com.qlk.odcinterface.sdk.api.request.OdcRequest;
import com.qlk.odcinterface.sdk.api.request.PmsCouponRequest;
import com.qlk.odcinterface.sdk.api.response.PmsCouponResponse;
import com.qlk.odcinterface.service.PmsCouponService;

/**
 * 优惠券REST服务：提供有关优惠券的接口
 * 
 * @author zhaoyanan
 * @version 2016-4-14 14:11:37
 * 
 */
@Component
@Path("services")
@Produces({ MediaType.APPLICATION_JSON })
public class PmsCouponResource {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    private PmsCouponService pmsCouponService; 

    /**
     * 查询优惠券信息
     * 
     * @param request
     *            优惠券请求参数
     * @return 优惠券返回对象
     * 
     */
    @POST
    @Path("/pmsCoupon/getPmsCoupon")
    public Wrapper<?> getPmsCoupon(PmsCouponRequest request) {
        if (null == request || null == request.getId()) {
            this.logger.error("getPmsCoupon 传入参数有误");
            return WrapMapper.illegalArgument();
        }

        try {
            PmsCoupon pmsCoupon = pmsCouponService.getPmsCouponById(request.getId());
            PmsCouponResponse response = convert(pmsCoupon);
            return WrapMapper.ok().result(response);
        } catch (Exception e) {
            this.logger.error("查询优惠券数据异常", e);
            return WrapMapper.error();
        }
    } 

    // 数据转换
    private PmsCouponResponse convert(PmsCoupon pmsCoupon) {
        if (null == pmsCoupon) {
            return null;
        }

        PmsCouponResponse pmsCouponResponse = new PmsCouponResponse();
        BeanUtils.copyProperties(pmsCoupon, pmsCouponResponse);
        return pmsCouponResponse;
    }

    // 数据转换
    private List<PmsCouponResponse> convertList(List<PmsCoupon> pmsCoupons) {
        if (CollectionUtils.isEmpty(pmsCoupons)) {
            return null;
        }

        List<PmsCouponResponse> list = new ArrayList<PmsCouponResponse>(pmsCoupons.size());
        for (PmsCoupon pmsCoupon : pmsCoupons) {
            list.add(convert(pmsCoupon));
        }
        return list;
    } 
    
    @POST
    @Path("/pmsCoupon/syncPmsCoupon")
    public Wrapper<?> syncPmsCoupon(@Form OdcRequest request){
    	try {
            if (null == request || StringUtils.isEmpty(request.getContent())) {
                this.logger.error("addBatchPmsCoupons 传入参数有误");
                return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_ ,"传入参数不能为空！");
            }
            
            Integer isStatusUpdate = null;
            if(StringUtils.isNotEmpty(request.getIsStatusUpdate())){
            	isStatusUpdate = Integer.parseInt(request.getIsStatusUpdate());
            }
            
            Integer preStatus = null;
            if(StringUtils.isNotEmpty(request.getPreStatus())){
            	preStatus = Integer.parseInt(request.getPreStatus());
            }
            
            String content = request.getContent();
            this.logger.debug("odc优惠券修改传入参数：---->" + JsonHelper.toJson(request));
            
            PmsCouponRequest couponRequest = JsonHelper.toBean(content, PmsCouponRequest.class);
            System.out.println(couponRequest);
            boolean flag = pmsCouponService.syncPmsCoupon(isStatusUpdate, couponRequest,preStatus);
            if(flag) return WrapMapper.ok();
            return WrapMapper.error();
        } catch (Exception e) {
            this.logger.error("更新odc数据异常", e);
            return WrapMapper.error();
        }
    }
}
